package com.blb.java12_jpa.dao;

import com.blb.java12_jpa.doman.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;

/**
 * 书籍的DAO接口
 */
@Repository
public interface BookRepository extends JpaRepository<Book,Long> {

    /**
     * 按名字做模糊查询
     * @param name
     * @return
     */
    List<Book> findBooksByNameContains(String name);

    /**
     * 按价格区间查询
     * @param min
     * @param max
     * @return
     */
    List<Book> findBooksByPriceBetween(BigDecimal min,BigDecimal max);

    /**
     * 自定义SQL
     */
    @Transactional
    @Modifying
    @Query(value = "update tb_book set price = ?1 where id = ?2",nativeQuery = true)
    int updatePriceById(BigDecimal price,Long id);

    @Query(value = "select * from tb_book where id = ?1",nativeQuery = true)
    Book findBookById(Long id);
}
